package com.ty.cloudpicture.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ty.cloudpicture.annotation.AuthCheck;
import com.ty.cloudpicture.common.BaseResponse;
import com.ty.cloudpicture.common.ErrorCode;
import com.ty.cloudpicture.common.ResultUtils;
import com.ty.cloudpicture.common.UserConstant;
import com.ty.cloudpicture.exception.BusinessException;
import com.ty.cloudpicture.exception.ThrowUtils;
import com.ty.cloudpicture.model.dto.space.SpaceAddRequest;
import com.ty.cloudpicture.model.dto.space.SpaceLevel;
import com.ty.cloudpicture.model.dto.space.SpaceQueryRequest;
import com.ty.cloudpicture.model.dto.space.SpaceUpdateRequest;
import com.ty.cloudpicture.model.dto.space.analyze.SpaceAnalyzeRequest;
import com.ty.cloudpicture.model.dto.space.analyze.SpaceRankAnalyzeRequest;
import com.ty.cloudpicture.model.dto.space.analyze.SpaceUsageAnalyzeRequest;
import com.ty.cloudpicture.model.dto.space.analyze.SpaceUserAnalyzeRequest;
import com.ty.cloudpicture.model.entity.Space;
import com.ty.cloudpicture.model.entity.User;
import com.ty.cloudpicture.model.enums.SpaceLevelEnum;
import com.ty.cloudpicture.model.vo.SpaceVO;
import com.ty.cloudpicture.model.vo.space.analyze.SpaceCategoryAnalyzeResponse;
import com.ty.cloudpicture.model.vo.space.analyze.SpaceTagAnalyzeResponse;
import com.ty.cloudpicture.model.vo.space.analyze.SpaceUsageAnalyzeResponse;
import com.ty.cloudpicture.model.vo.space.analyze.SpaceUserAnalyzeResponse;
import com.ty.cloudpicture.service.SpaceAnalyzeService;
import com.ty.cloudpicture.service.SpaceService;
import com.ty.cloudpicture.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/space/analyze")
@Api(tags = "空间分析管理")
@AllArgsConstructor
public class SpaceAnalyzeController {
    private final UserService userService;
    private final SpaceAnalyzeService spaceAnalyzeService;
    @PostMapping("/usage")
    @ApiOperation(value = "空间使用情况分析")
    public BaseResponse<SpaceUsageAnalyzeResponse> getSpaceUsageAnalyze(@RequestBody SpaceUsageAnalyzeRequest spaceUsageAnalyzeRequest,HttpServletRequest request){
        ThrowUtils.throwIf(spaceUsageAnalyzeRequest==null, ErrorCode.PARAMS_ERROR, "参数为空");
        User loginUser = userService.getLoginUser(request);
        SpaceUsageAnalyzeResponse spaceUsageAnalyzeResponse = spaceAnalyzeService.getSpaceUsageAnalyze(spaceUsageAnalyzeRequest, loginUser);
        return ResultUtils.success(spaceUsageAnalyzeResponse);
    }
    @PostMapping("/category")
    @ApiOperation(value = "空间图片分类情况分析")
    public BaseResponse<List<SpaceCategoryAnalyzeResponse>> getSpaceCategoryAnalyze(@RequestBody SpaceAnalyzeRequest spaceAnalyzeRequest,HttpServletRequest request){
        ThrowUtils.throwIf(spaceAnalyzeRequest==null, ErrorCode.PARAMS_ERROR, "参数为空");
        User loginUser = userService.getLoginUser(request);
        List<SpaceCategoryAnalyzeResponse> spaceCategoryAnalyzeResponse = spaceAnalyzeService.getSpaceCategoryAnalyze(spaceAnalyzeRequest, loginUser);
        return ResultUtils.success(spaceCategoryAnalyzeResponse);
    }
    @PostMapping("/tag")
    @ApiOperation(value = "空间图片标签情况分析")
    public BaseResponse<List<SpaceTagAnalyzeResponse>> getSpaceTagAnalyze(@RequestBody SpaceAnalyzeRequest spaceAnalyzeRequest, HttpServletRequest request){
        ThrowUtils.throwIf(spaceAnalyzeRequest==null, ErrorCode.PARAMS_ERROR, "参数为空");
        User loginUser = userService.getLoginUser(request);
        List<SpaceTagAnalyzeResponse> spaceTagAnalyze = spaceAnalyzeService.getSpaceTagAnalyze(spaceAnalyzeRequest, loginUser);
        return ResultUtils.success(spaceTagAnalyze);
    }
    @PostMapping("/user")
    @ApiOperation(value = "空间用户情况分析")
    public BaseResponse<List<SpaceUserAnalyzeResponse>> getSpaceUserAnalyze(@RequestBody SpaceUserAnalyzeRequest spaceUserAnalyzeRequest, HttpServletRequest request){
        ThrowUtils.throwIf(spaceUserAnalyzeRequest==null, ErrorCode.PARAMS_ERROR, "参数为空");
        User loginUser = userService.getLoginUser(request);
        List<SpaceUserAnalyzeResponse> spaceTagAnalyze = spaceAnalyzeService.getSpaceUserAnalyze(spaceUserAnalyzeRequest, loginUser);
        return ResultUtils.success(spaceTagAnalyze);
    }
    @PostMapping("/rank")
    @ApiOperation(value = "空间排名情况分析")
    public BaseResponse<List<Space>> getSpaceRankAnalyze(@RequestBody SpaceRankAnalyzeRequest spaceAnalyzeRequest, HttpServletRequest request){
        ThrowUtils.throwIf(spaceAnalyzeRequest==null, ErrorCode.PARAMS_ERROR, "参数为空");
        User loginUser = userService.getLoginUser(request);
        List<Space> spaceRankAnalyze = spaceAnalyzeService.getSpaceRankAnalyze(spaceAnalyzeRequest, loginUser);
        return ResultUtils.success(spaceRankAnalyze);
    }
}
